package com.a0soft.gphone.aTruffleHog.wnd.stat;
/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Copyright(C) 2011 
 *   Sam Lu <ysl.sam.lu (at) gmail.com>
 *
 */

import java.text.DecimalFormat;

import com.a0soft.gphone.aTruffleHog.R;
import com.a0soft.gphone.aTruffleHog.gio.ui.BaseActivity;
import com.a0soft.gphone.aTruffleHog.pref.Pref;
import com.a0soft.gphone.aTruffleHog.util.aDebug;
import com.a0soft.gphone.aTruffleHog.widget.RangeSeekBar;
import com.a0soft.gphone.aTruffleHog.widget.RangeSeekBar.OnRangeSeekBarChangeListener;
import com.a0soft.gphone.aTruffleHog.wnd.BaseFragment;
import com.a0soft.gphone.aTruffleHog.wnd.info.InfoViewCONSTS;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FindDevicesByCPUFormFrg extends BaseFragment {
    public static final int CORE_NUM_IDX_MIN = 1; 
    public static final int CORE_NUM_IDX_MAX = 8; 
    public static final int CPU_SPEED_IDX_MIN = 0; //400MHZ
    public static final int CPU_SPEED_IDX_MAX = 18; //4GHZ
    
    private static final String EXTRA_PREFIX = FindDevicesByCPUFormFrg.class.getName();
    public static final String EXTRA_CORE_NUM_MIN = EXTRA_PREFIX + ".cnmin";
    public static final String EXTRA_CORE_NUM_MAX = EXTRA_PREFIX + ".cnmax";
    public static final String EXTRA_CPU_SPEED_MIN = EXTRA_PREFIX + ".csmin";
    public static final String EXTRA_CPU_SPEED_MAX = EXTRA_PREFIX + ".csmax";
    
    private TextView m_tvMinCore, m_tvMaxCore;
    private TextView m_tvMinCpu, m_tvMaxCpu;
    
    private RangeSeekBar<Integer> m_sbCore, m_sbCpu;
    
    private static final String LOG_TAG = FindDevicesByCPUFormFrg.class.getSimpleName();
    
    ////////////////////////////////////////
    public static FindDevicesByCPUFormFrg 
    NewInstance() 
    {
        FindDevicesByCPUFormFrg frg = new FindDevicesByCPUFormFrg();
        return frg;
    }
    
    @Override public View 
    onCreateView(LayoutInflater inflater, ViewGroup vgContainer,
                 Bundle bdSavedInstanceState) 
    {
        if (aDebug.ENABLED)
            aDebug.d(LOG_TAG, "onCreateView()");
        
        Context ctx = getActivity();
        
        View vRoot = inflater.inflate(R.layout.find_device_by_cpu_form_frg, vgContainer, false);
        
        m_tvMinCore = (TextView)vRoot.findViewById(R.id.min_core_num);
        m_tvMaxCore = (TextView)vRoot.findViewById(R.id.max_core_num);
        m_tvMinCpu = (TextView)vRoot.findViewById(R.id.min_cpu_speed);
        m_tvMaxCpu = (TextView)vRoot.findViewById(R.id.max_cpu_speed);
        
        //////////////////////////////////////////////////////////////////
        RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(
                CORE_NUM_IDX_MIN, CORE_NUM_IDX_MAX, ctx);
        m_sbCore = seekBar;
        seekBar.setNotifyWhileDragging(true);
        seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
            @Override public void 
            onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, 
                                        Integer minValue, Integer maxValue)
            {
                _UpdateCoreSeekBar();
            }
        });
        
        seekBar.setSelectedMaxValue(Pref.GetMaxCoreNumIdx(ctx));
        seekBar.setSelectedMinValue(Pref.GetMinCoreNumIdx(ctx));
        
        ViewGroup layout = (ViewGroup)vRoot.findViewById(R.id.container_core_num);
        layout.addView(seekBar);
        
        //////////////////////////////////////////////////////////////////
        seekBar = new RangeSeekBar<Integer>(
                CPU_SPEED_IDX_MIN, CPU_SPEED_IDX_MAX, ctx);
        m_sbCpu = seekBar;
        seekBar.setNotifyWhileDragging(true);
        seekBar.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {
            @Override public void 
            onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, 
                                        Integer minValue, Integer maxValue)
            {
                _UpdateCpuSeekBar();
            }
        });
        
        seekBar.setSelectedMaxValue(Pref.GetMaxCpuSpeedIdx(ctx));
        seekBar.setSelectedMinValue(Pref.GetMinCpuSpeedIdx(ctx));
        
        layout = (ViewGroup)vRoot.findViewById(R.id.container_cpu_speed);
        layout.addView(seekBar);
        
        //////////////////////////////////////////////////////////////////
        vRoot.findViewById(R.id.find).setOnClickListener(new View.OnClickListener() {            
            @Override public void 
            onClick(View v)
            {
                Context ctx = v.getContext();
                int nMinCoreIdx = m_sbCore.getSelectedMinValue();
                int nMaxCoreIdx = m_sbCore.getSelectedMaxValue();
                Pref.SetMinCoreNumIdx(ctx, nMinCoreIdx);
                Pref.SetMaxCoreNumIdx(ctx, nMaxCoreIdx);

                int nMinCpuIdx = m_sbCpu.getSelectedMinValue();
                int nMaxCpuIdx = m_sbCpu.getSelectedMaxValue();
                Pref.SetMinCpuSpeedIdx(ctx, nMinCpuIdx);
                Pref.SetMaxCpuSpeedIdx(ctx, nMaxCpuIdx);
                
                BaseActivity activity = (BaseActivity)getActivity();
                if (activity != null) {
                    Intent it = new Intent();
                    it.putExtra(EXTRA_CORE_NUM_MIN, nMinCoreIdx);
                    it.putExtra(EXTRA_CORE_NUM_MAX, nMaxCoreIdx);
                    it.putExtra(EXTRA_CPU_SPEED_MIN, _GetCpuSpeed(nMinCpuIdx, true));
                    it.putExtra(EXTRA_CPU_SPEED_MAX, _GetCpuSpeed(nMaxCpuIdx, false));
                    
                    activity.notifyByFragment(FindDevicesByCPUFormFrg.this, it);
                }
            }
        });
        
        _UpdateCoreSeekBar();
        _UpdateCpuSeekBar();
        return vRoot;
    }
    
    private void
    _UpdateCoreSeekBar()
    {
        int nMin = m_sbCore.getSelectedMinValue();
        int nMax = m_sbCore.getSelectedMaxValue();
        m_tvMinCore.setText(Integer.toString(nMin));
        m_tvMaxCore.setText(Integer.toString(nMax));
    }
    
    private void
    _UpdateCpuSeekBar()
    {
        int nMin = m_sbCpu.getSelectedMinValue();
        int nMax = m_sbCpu.getSelectedMaxValue();
        m_tvMinCpu.setText(_GetCpuSpeedText(nMin));
        m_tvMaxCpu.setText(_GetCpuSpeedText(nMax));
    }
    
    private int
    _GetCpuSpeed(int nIdx, boolean bForMin)
    {
        int n = _GetCpuSpeed(nIdx);
        if (bForMin) {
            n -= (100000);
        }
        else n += (100000);
        
        return n;
    }
    
    private int
    _GetCpuSpeed(int nIdx)
    {
        return nIdx * 200000 + 400000;
    }
    
    private String
    _GetCpuSpeedText(int nIdx)
    {
        int nSpeedInKHz = _GetCpuSpeed(nIdx);
        long nSpeedInGHz = nSpeedInKHz / InfoViewCONSTS.GHZ_LV; 
        if (nSpeedInGHz > 0) {
            double dxVal = nSpeedInKHz / (double)InfoViewCONSTS.GHZ_LV;
            return new DecimalFormat("#.##").format(dxVal) + " " + 
                    getActivity().getResources().getString(R.string.di_cp_ghz);
        }
        else {
            double dxVal = nSpeedInKHz / (double)InfoViewCONSTS.MHZ_LV;
            return new DecimalFormat("#.##").format(dxVal) + " " + 
                    getActivity().getResources().getString(R.string.di_cp_mhz);
        }
    }
}
